<?php
/*
 * Created on Dec 4, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
global $AppUI, $m, $tab;

// Get login object
$uid = $AppUI->user_id;
// Check permission
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canAccess = $perms->checkModule($m, 'access');
$canEdit = $perms->checkModule($m, 'edit');
$denyDelete = !getPermission($m, 'delete');

// check permission list quotations
if(!$canAccess){
	$AppUI->redirect(PUBLIC_PAGE);
}

require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getModuleClass('suppliers'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));
require_once($AppUI->getMasterDataClass('mdcurrency'));

// Check action is search or all or first time
if(isset($_POST['allAction'])){
	$_POST = array();
}

$quote_status = intval( dPgetParam( $_POST, 'quote_status', 0 ) );
$quote_type = intval( dPgetParam( $_POST, 'quote_type', 0 ) );
$quote_customer = intval( dPgetParam( $_POST, 'quote_customer', 0 ) );
$quote_shipping_type = intval( dPgetParam( $_POST, 'quote_shipping_type', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );
$quote_name = dPgetParam( $_POST, 'quote_name', '' );
$quote_code = dPgetParam( $_POST, 'quote_code', '' );
$flagResult = -1;

$where = " 1=1";
if ($quote_status != 0) {
	$where .= " AND quote_status = $quote_status";
}
if ($quote_type != 0) {
	$where .= " AND quote_type = $quote_type";
}
if ($quote_customer != 0) {
	$where .= " AND quote_customer = $quote_customer";
}
if ($quote_shipping_type != 0) {
	$where .= " AND quote_shipping_type = $quote_shipping_type";
}
if ($quote_name != "") {
	$where .= " AND quote_name LIKE '%$quote_name%'";
}
if ($quote_code != "") {
	$where .= " AND quote_code LIKE '%$quote_code%'";
}
if ($keyword != "") {
	$where .= " AND (quote_code LIKE '%$keyword%' or quote_name LIKE '%$keyword%')";
}

// Load Object Status
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Quotation');

$objMD = new CMdType();
$shipTypeList = $objMD->getTypeList('ShippingType');
//$shipTypeList = arrayMerge(array('0'=>''), $shipTypeList);
$shipTypeList = $shipTypeList;

$typeList = $objMD->getTypeList('Quotation');
//$typeList = arrayMerge(array('0' => ''), $typeList);
$typeList = $typeList;

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>$AppUI->_('')), $cusList);
?>
<form name="searchFrm" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
<input type="hidden" name="offset">
<table cellspacing="0" cellpadding="3" border="0" width="100%" class="infopanel">
<tr>
	<td>
		<input class="button" type="submit" name="searchAction" value="<?=$AppUI->_('search');?>"/>
		<input class="button" type="submit" name="allAction" value="<?=$AppUI->_('All');?>"/>
	</td>
</tr>
</table>
<table cellspacing="0" cellpadding="3" border="0" width="100%" class="subinfopanel">
<tr>
	<td align="right"><?=$AppUI->_('Code')?></td>
	<td>
		<input type="text" class="text" name="quote_code" id="quote_code" value="<?=$quote_code?>" maxlength="50"/>
	</td>
	<td align="right"> <?=$AppUI->_('Status')?></td>
	<td width="70%">
		<?= arraySelect($statList, 'quote_status', ' id="quote_status" size="1" class="text" style="width: 135px;"', $quote_status, true)?>
	</td>
</tr>
<tr>
	<td nowrap="nowrap" align="right"><?=$AppUI->_('Name')?></td>
	<td>
		<input type="text" class="text" name="quote_name" id="quote_name" value="<?=$quote_name?>" maxlength="50"/>
	</td>
	<td align="right"><?=$AppUI->_('Type')?></td>
	<td><?= arraySelect($typeList, 'quote_type', ' id="quote_type" size="1" class="text" style="width: 135px;"', $quote_type, true)?></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Customer')?></td>
	<td><?= arraySelect($cusList, 'quote_customer', ' id="quote_customer" size="1" class="text" style="width: 200px;"', $quote_customer, true)?></td>
	<td nowrap="nowrap" align="right"><?=$AppUI->_('Shipping Type')?></td>
	<td>
		<?= arraySelect($shipTypeList, 'quote_shipping_type', ' id="quote_shipping_type" size="1" class="text" style="width: 135px;"', $quote_shipping_type, true)?>
	</td>
</tr>
</table>
</form>
<?php
	// paging
	require("modules/ticketsmith/common.inc.php");
	$limit = 20;
	$offset = intval( dPgetParam( $_POST, 'offset', 0 ) );;
	$totalRows = 0;
	$paging = "";

	$query = "SELECT COUNT(*) FROM quotations WHERE $where";
	$totalRows = query2result($query);

	$q  = new DBQuery;
	$q->createTemp('shipping_types');
	$q->addTable('md_type');
	$q->addQuery('type_id, name as shipping_type_name');
	$q->addWhere("type = 'ShippingType'");
	$q->exec();
	$q->clear();


	$q->addTable('quotations', 'q');
	$q->addQuery('quote_id, quote_code, quote_name, quote_status, quote_type, quote_customer, quote_shipping_type, t.name, s.status_name, co.company_name, t2.shipping_type_name');
	$q->addJoin('md_type', 't', 't.type_id = q.quote_type');
	$q->addJoin('shipping_types', 't2', 't2.type_id = q.quote_shipping_type');
	$q->addJoin('md_status', 's', 's.status_id = q.quote_status');
	$q->addJoin('companies', 'co', 'co.company_id = q.quote_customer');
	$q->addWhere($where);

	// paging
	$q->setLimit($limit,$offset);
	$objResult = $q->loadList();
	// Remove table temp
	$q->dropTemp('shipping_types');
	$q->exec();

	$i = 0;
	?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.searchFrm.offset.value = offset;
			document.searchFrm.submit();
		}
	</script>
	<table id="idQuotationList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th><?=$AppUI->_('Code')?></th>
			<th><?=$AppUI->_('Name')?></th>
			<th><?=$AppUI->_('Status')?></th>
			<th><?=$AppUI->_('Type')?></th>
			<th><?=$AppUI->_('Customer')?></th>
			<th><?=$AppUI->_('Shipping type')?></th>
		</tr>
	<?php
	foreach ($objResult as $row) {
		$i++;
		?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>" onClick="showQuotation(<?=$row['quote_id']?>)">
			<td><?=$row['quote_code']?></td>
			<td><?=$row['quote_name']?></td>
			<td><?=$row['status_name']?></td>
			<td><?=$row['name']?></td>
			<td><?=$row['company_name']?></td>
			<td><?=$row['shipping_type_name']?></td>
		</tr>
		<?php
	}
	?>
	</table>
	<table border="0" cellpadding="2" cellspacing="0" width="100%">
		<tr>
			<td nowrap>
			<?php
			// paging
			if (($offset + $limit) < $totalRows) {
			    $page_string = ($offset + 1) . " to " . ($offset + $limit) . " of $totalRows";
			}
			else {
			    $page_string = ($offset + 1) . " to $totalRows of $totalRows";
			}
			if ($totalRows > $limit) {
				$offset1 = $offset - $limit;
				$offset2 = $offset + $limit;
			    if ($offset1 >= 0) {
			        print("<a href=# onclick='submitForm($offset1);'>".$AppUI->_('Prev')."</a> | \n");
			    }
			    print($AppUI->_("$page_string")."\n");
			    if ($offset2 < $totalRows) {
			        print(" | <a href=# onclick='submitForm($offset2);'>".$AppUI->_('Next')."</a>\n");
			    }
			}
			?>
			</td>
		</tr>
	</table>
	<script>
		Calendar.enabled = true;
		Table("idQuotationList").setNormalHover(1);
		function showQuotation(id){
			location.href = "index.php?m=quotations&a=quote_view&quote_id=" + id;
		}
	</script>